#include "dialog_gh_env.h"
#include "ui_dialog_gh_env.h"
#include <QMessageBox>
#include <QSettings>
#include <QPrinter>
#include <QPrinterInfo>
#include "pub_var.h"

Dialog_gh_env::Dialog_gh_env(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog_gh_env)
{
    ui->setupUi(this);

    //Printers list
    ui->cbbPrinters->clear();
    ui->cbbPrinters_2->clear();
    ui->cbbPrinters_3->clear();
    ui->cbbPrinters_4->clear();
    QList<QPrinterInfo> list = QPrinterInfo::availablePrinters();
    for(int i = 0 ;i<list.size();i++){
        ui->cbbPrinters->addItem(list.at(i).printerName());
        ui->cbbPrinters_2->addItem(list.at(i).printerName());
        ui->cbbPrinters_3->addItem(list.at(i).printerName());
        ui->cbbPrinters_4->addItem(list.at(i).printerName());
    }
    QSettings setIni("clin.ini", QSettings::IniFormat);
    ui->cbbPrinters->setCurrentText(setIni.value("gh/printer").toString());
    ui->cbbPrinters_2->setCurrentText(setIni.value("sk/printer").toString());
    ui->cbbPrinters_3->setCurrentText(setIni.value("cfdy/printer").toString());
    ui->cbbPrinters_4->setCurrentText(setIni.value("bz/printer").toString());

    QRect rct = setIni.value("windowState/gh_env").toRect();
    this->setGeometry(rct);

    QString yb = setIni.value("insurance/insurance").toString();
    QString sfz = yb;
    //QString sfz = setIni.value("cardreader/use").toString();
    if(yb == "1"){
        ui->cbYb->setChecked(true);
    }else{
        ui->cbYb->setChecked(false);
    }
    if(sfz == "1"){
        ui->cbSfz->setChecked(true);
    }else{
        ui->cbSfz->setChecked(false);
    }

    QString cardDriver = setIni.value("insurance/carddriver").toString();
    for(int i = 0; i < ui->cbbCardDriver->count(); i++){
        if(pub::getStr(ui->cbbCardDriver->itemText(i), " ",1) == cardDriver ){
            ui->cbbCardDriver->setCurrentIndex(i);
            break;
        }
    }

    ui->cbSfz->setVisible(false);   //新环境 ， 身份证无单独设置

    connect(ui->pbSave,&QPushButton::clicked,this,&Dialog_gh_env::pbSave_clicked);
    connect(ui->pbExit,&QPushButton::clicked,this,&Dialog_gh_env::pbExit_clicked);
}

Dialog_gh_env::~Dialog_gh_env()
{
    delete ui;
}

void Dialog_gh_env::pbSave_clicked()
{
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("gh/printer", ui->cbbPrinters->currentText());
    setIni.setValue("sk/printer", ui->cbbPrinters_2->currentText());
    setIni.setValue("cfdy/printer", ui->cbbPrinters_3->currentText());
    setIni.setValue("bz/printer", ui->cbbPrinters_4->currentText());
    setIni.setValue("insurance/insurance", (ui->cbYb->isChecked() ? "1" : "0"));
    setIni.setValue("cardreader/use", (ui->cbSfz->isChecked() ? "1" : "0"));
    setIni.setValue("insurance/carddriver", pub::getStr(ui->cbbCardDriver->currentText(), " ",1));

    QMessageBox::information(this,"注意","默认设置保存完成！");
}

void Dialog_gh_env::pbExit_clicked()
{
    this->close();
}

void Dialog_gh_env::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/gh_env", QVariant(this->geometry()));
}
